<%@ page language="java" import="java.util.*" import="java.sql.*" import = "orm.*"  pageEncoding="utf-8" errorPage="errorPage.jsp"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% 	String courseOpt = ""; 
	String knowledgesOpt = "";
	List courses = (List)request.getAttribute("courseList");
	Map knowledgeMap = (Map)session.getAttribute("knowledgeMap");
	if (courses == null || courses.size() == 0){
		courseOpt = "<option value=''>您没有课程！</option>";
	}
	else {
		for(int i = 0; i < courses.size(); i++){
			Course course = (Course)courses.get(i);
			String name = course.getCoursename();
			if (i == 0){
				courseOpt += "<option value = '" + name + "' selected='selected' >" + name + "</option>";
				Map knowledges = (Map)knowledgeMap.get(name);
				knowledgesOpt = "";
				if (knowledges != null && knowledges.size() > 0){
					Set kIds = knowledges.keySet();
					Iterator it = kIds.iterator();
					for (int j = 1; it.hasNext(); j++){
						Long o = (Long)it.next();
						Knowledge k = (Knowledge)knowledges.get(o);
						if (j == 1){
							knowledgesOpt += "<option selected = 'selected' value = '" + o + "'>" + k.getKname() + "</option>";
							continue;
						}
						knowledgesOpt += "<option value = '" + o + "'>" + k.getKname() + "</option>";
					}
					session.setAttribute("knowledges", knowledges);
				}
				else{
					knowledgesOpt = "<option value=''>该课程下没有知识点！</option>";
					session.removeAttribute("knowledges");
				}
				continue;
			}
			courseOpt += "<option value = '" + name + "'>" + name + "</option>";
		}
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href = "<%=basePath %>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>知识点管理</title>
<link href="css/tStyles.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="js/AjaxRequest.js">
</script>
<script type="text/javascript">
	<%if (request.getAttribute("success") != null){%>alert("<%=request.getAttribute("success")%>");<%request.removeAttribute("success");}
	else if (request.getAttribute("error") != null){%>alert("<%=request.getAttribute("error")%>");<%request.removeAttribute("error");}%>
	function showKnowledges(){
		c = this.course.value;
		var loader=new net.AjaxRequest("ajax.do?action=getKnowledges&c=" + c,show,onerror,"GET");
	}
	function display(){
		var loader2=new net.AjaxRequest("ajax.do?action=knowledges_display",displayButton,onerror,"GET");
	}
	function onerror(){
	}
	function show(){
		document.getElementById("st_2").innerHTML=this.req.responseText;
		display();
	}
	function displayButton(){
		var str = this.req.responseText;
		if (str.indexOf("display")!= -1){
			document.getElementById("modifyK").style.visibility = 'visible';
			document.getElementById("deleteK").style.visibility = 'visible';
		}
		else{
			document.getElementById("modifyK").style.visibility = 'hidden';
			document.getElementById("deleteK").style.visibility = 'hidden';
		}
	}
	function operateKnowledges(str){
		if (str.indexOf("addK") != -1){
			var newKnow = prompt("请输入知识点名称：", "");
			if (newKnow){
				window.location.href = "manageKnowledges.do?action=knowledges&method=add&course=" 
					+ this.course.value + "&know=" + encodeURIComponent(newKnow);
			}
		}
		else if (str.indexOf("modifyK") != -1){
			var know = prompt("请输入知识点名称：", 
				document.getElementById("st_2").options[document.getElementById("st_2").selectedIndex].text);
			if (know){
				window.location.href = "manageKnowledges.do?action=knowledges&method=view&course=" 
				+ this.course.value + "&know=" + encodeURIComponent(know) + "&id=" + this.st_2.value;
			}
		}
		else if (str.indexOf("deleteK") != -1){
			var opt = confirm("本次删除不可恢复，是否删除该模板?");
			if (opt == true){
				window.location.href = "manageKnowledges.do?action=deleteKnowledge&id=" + this.st_2.value;
			}
		}
	}
</script>
</head>
<% 	User tea  =(User) session.getAttribute("Teacher");  
	if (tea == null){
		response.sendRedirect(path + "/index.jsp");
	}
%> 

<body>
	<!-- CONTAINER START -->
<div id = "container">
<!-- HEADER START -->
        <div id = "top">
        	<div id = "navigation">
            	<html:link page="/teacher/teacher.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;首页</html:link>
                <html:link page="/teacher/notice.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;通知</html:link>
                <html:link action="manageQuestions.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;题库</html:link>
                <html:link action="manageKnowledges.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;知识点</html:link>
                <html:link action="manageTemplates.do?action=startManagement" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;试卷模板</html:link>
                <html:link action="gradeManagement.do?action=startMana" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;成绩管理</html:link>
                <html:link page="/teacher/studentMana.jsp" styleClass = "link">&nbsp;&nbsp;&nbsp;&nbsp;学生管理</html:link>
            </div>
        </div>
        <!-- HEADER END -->
        
	<!-- LOGIN START -->
        <div id = "login">
        	<div id = "name">
        	<%=tea.getRealname()%>
            </div>
        	<html:link page="/teacher/teacherInfo.jsp" styleClass="viewInfo">·查询个人信息</html:link>
          	<p><html:link page="/teacher/teaModifyPwd.jsp" styleClass="modifyPwd">·修改密码</html:link></p>
            <p><html:link page="/teacher/letter.jsp" styleClass="letter">·站内信</html:link></p>
            <p><html:link forward="logout" styleClass="logout">·注销登录</html:link></p>
  		</div>
        <!-- LOGIN END -->
        
        <!-- CLOCK START -->
  		<div id = "clock">
        	<iframe src="index/clock.html" height="407" width="296" frameborder="0" scrolling="no"
            		marginheight="0" marginwidth="0" id="clockFrame" name="clockFrame"></iframe>
		</div>
        <!-- CLOCK END -->
        
        <!-- MAIN START -->
      	<div id = "main">
        	<center>
        	<div id = "subject">
            	<br /><br />
            	<span class="text"><strong>&nbsp;&nbsp;科目:</strong></span>
              		<select name="course" id="course" style="width:100px;" 
              		onchange="javascript:showKnowledges();">
              			<%=courseOpt %>
             		</select>
            </div>
            <div id = "knowledge">
       	  		<table border="1">
                	<tr>
    					<td width="332" height="28" class="st_text"><strong>已有知识点</strong></td>
  					</tr>
  					<tr>
    					<td height="278" valign="top">
                          <select name="st_1" size="20" id="st_2" style="width:300px; ">
                            <%=knowledgesOpt %>
                          </select></td>
  					</tr><% if (courses != null && courses.size() != 0){ %>
  					<tr>
    					<td height="41" align="center">
                        	<input type="button" name="addK" id="addK" value="添加知识点" style="width:80px; height:40px"
                        	onclick="operateKnowledges(this.name);" />
      						&nbsp;
        					<input type="button" name="modifyK" id="modifyK" value="修改" style="width:80px; height:40px;
        					<%if (session.getAttribute("knowledges") == null) {%>visibility:hidden;<%} %>"
        					onclick="operateKnowledges(this.name);"/>
                            &nbsp;
                            <input type="button" name="deleteK" id="deleteK" value="删除" style="width:80px; height:40px;
                            <%if (session.getAttribute("knowledges") == null) {%>visibility:hidden;<%} %>"
                            onclick="operateKnowledges(this.name);"/></td>
                    </tr><%} %>
              </table>
            </div>
            </center>
   	  	</div>
	<!-- MAIN END -->
        
	<!-- FOOTER START -->
	<div id = "footer">
  	</div>
	<!-- FOOTER END -->
        
</div>
    <!-- CONTAINER END -->
</body>
</html>